import workforecast.*

class BootStrap {

    def init = { servletContext ->
		
		if (!BusinessUnit.findByName("test-businessunit")) {
			
			// Add a new BusinessUnit (A Team can't be created without a BusinessUnit, because of a foreign key in the DB)
			BusinessUnit bu = new BusinessUnit(name: "test-businessunit")
			if (bu.save()) System.out.println("Business unit \"test-businessunit\" created")
			
			// Add a new Team (A User can't be created without a Team, because of a foreign key in the DB)
			Team team = new Team(name: "test-team", businessUnit: bu)
			if (team.save()) System.out.println("Team \"test-team\" created")
			
			// Roles
			Role[] roles = [
				new Role(name: "Director"),
				new Role(name: "Manager"),
				new Role(name: "Consultant")
			]
			
			for (Role role : roles) {
				// Add Roles
				if (role.save()) {
					System.out.println("Role \"" + role.name + "\" added")
					
					// Add a new User for testing purposes
					User user = new User(username: "test-"+role.name, password: "password", team: team, role: role)
					if (user.save()) System.out.println("User named: \"" + user.username + "\" created with a role: \"" + user.role.name +"\"")
				}
			}
		}
    }
	
    def destroy = {
    }
}
